package automation {
import util.SeleFxCodecHelper;
import util.SeleFxCodecHelper;
import flash.events.Event;
import flash.system.ApplicationDomain;
import flash.utils.describeType;
import mx.automation.IAutomationManager;
import mx.automation.IAutomationMethodDescriptor;
import mx.automation.IAutomationObject;
import mx.core.mx_internal;

use namespace mx_internal;

public class SeleFxMethodDescriptor implements IAutomationMethodDescriptor {

    private var _name:String;
    private var _returnType:String;
    private var _asMethodName:String;
    private var _codecName:String;
    private var _args:Array;

    public function SeleFxMethodDescriptor(name:String, asMethodName:String, returnType:String, codecName:String, args:Array) {
        super();

        _name = name;
        _asMethodName = asMethodName;
        _returnType = returnType;
        _codecName = codecName;
        _args = args;
    }

    public function get name():String {
        return _name;
    }

    public function get returnType():String {
        return _returnType;
    }

    public function get codecName():String {
        return _codecName;
    }

    public function getArgDescriptors(obj:IAutomationObject):Array {
        return _args;
    }

    public function record(target:IAutomationObject, event:Event):Array {
        throw new Error("No support to record a method..");
    }    

    public function replay(target:IAutomationObject, args:Array):Object {
        /*
         var f:Function = Object(target)[asMethodName];
         var retVal:Object = f.apply(target, args);
         return retVal;
         */
        var argDescriptors:Array = getArgDescriptors(target);
        var asArgs:Object = {};

        var helper:SeleFxCodecHelper = SeleFxCodecHelper.getCodecHelper();
        helper.decodeProperties(asArgs, args, argDescriptors, target);

        // Convert args into an ordered array.
        var asArgsOrdered:Array = [];
        for (var argNo:int = 0; argNo < argDescriptors.length; ++argNo)
            asArgsOrdered.push(asArgs[argDescriptors[argNo].name]);

        var retVal:Object = super.replay(target, asArgsOrdered);

        return helper.encodeValue(retVal, _returnType, _codecName, target);
    }
}
}